<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Configuration;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ConfigurationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Configuration(), function (Grid $grid) {
            $grid->model()->whereNotIn('key', ['sign_up_credit', 'sign_in_reward', 'sign_in', 'bind_address_amount', 'superior_anti_water'])->orderByDesc('id');
            $grid->column('id');
            $grid->column('type');
            $grid->column('key');
            $grid->column('value')->display(function ($var){
                if ($this->type == "html"){
                    return "请进入详情查看";
                }else{
                    return $var;
                }
            });
            $grid->column('describe');


            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableFilterButton();
            $grid->disableRowSelector();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Configuration(), function (Form $form) {

            $form->model()->value2 = $form->model()->value;
            $form->display('id');
            $form->select('type')
                ->when('=','html', function (Form $form) {
                    $form->editor('value');
                })
                ->when(['bool','string','int','array','json','money','text'], function (Form $form) {
                    $form->textarea('value2')->help('数组逗号分隔');
                })
                ->options([
                    'bool' => '布尔',
                    'string' => '字符串',
                    'int' => '整型',
                    'array' => '数组',
                    'json' => 'json',
                    'money' => '金额',
                    'text' => '文本',
                    'html' => '富文本'
                ])
                ->default('bool');



            if ($form->isCreating()) {
                $form->text('key')->required();
            } else {
                $form->text('key')->readOnly();
            }

            //$form->text('value')->required()->help('数组逗号分隔');

            $form->text('describe')->required();

            $form->submitted(function (Form $form) {
                if($form->type != 'html'){
                    $form->value = $form->value2;
                }

                // 删除用户提交的数据
                $form->deleteInput('value2');
                unset($form->value2);
                unset($form->model()->value2);
            });


        });
    }
}
